<?php
class daka_classAction extends backendAction
{
    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('daka_class');
    }
    public function index() {
		$id = $this->_get('menupid','intval');
		$menuid = D('menu')->get_menuid('daka_class_list',$id);
		$this->assign('menuid', $menuid);
		
		$list = $this->_mod->order('cat_id asc,ordid asc')->relation(true)->select();
		$this->assign('list', $list);
		$this->display();
    }
	
	public function _before_add()
    {
        $tree = new Tree();
        $result = M('daka_class_cat')->where(array('status'=>1))->select();
        $array = array();
        foreach($result as $r) {
            $r['selected'] = $r['id'] == $_GET['pid'] ? 'selected' : '';
            $array[] = $r;
        }
        $str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $tree->init($array);
        $select_menus = $tree->get_tree(0, $str);
        $this->assign('select_menus', $select_menus);
    }

    public function _before_edit()
    {
        $id = $this->_get('id','intval');
        $info = $this->_mod->find($id);
        $this->assign('info', $info);
        $tree = new Tree();
        $result = M('daka_class_cat')->where(array('status'=>1))->select();
        $array = array();
        foreach($result as $r) {
            $r['selected'] = $r['id'] == $info['cat_id'] ? 'selected' : '';
            $array[] = $r;
        }
        $str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $tree->init($array);
        $select_menus = $tree->get_tree(0, $str);
        $this->assign('select_menus', $select_menus);
    }
	/**
     * 删除
     */
    public function delete()
    {
        $mod = D($this->_name);
        $pk = $mod->getPk();
        $ids = trim($this->_request($pk), ',');
        if ($ids) {
			if(in_array(1,explode(',',$ids))){
				$this->error('操作失败,新人课堂不允许删除');
			}
            if (false !== $mod->delete($ids)) {
                $this->success('操作成功',U($this->_name.'/index',array('menuid'=>$_GET['menuid'],'menupid'=>$_GET['menupid'],'class_id'=>$_GET['class_id'])));
            } else {
                $this->error('操作失败');
            }
        } else {
            $this->error('操作失败');
        }
    }

}